package com.restaurant.validators;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.restaurant.domain.Inventory;
import com.restaurant.domain.Menuinventory;
import com.restaurant.domain.Modifireinventory;
import com.restaurant.service.MenuinventoryService;
import com.restaurant.service.ModifireinventoryService;

public class InventoryDeleteFormValidator implements Validator{

	@Autowired
	private MenuinventoryService objMenuinventoryService;
	
	@Autowired
	private ModifireinventoryService objModifireinventoryService;
	
	@Override
	public boolean supports(Class<?> arg0) {
		return Inventory.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Inventory inventory = (Inventory) arg0;
		ArrayList<Menuinventory> listmMenuinventories = new ArrayList<Menuinventory>();
		
		try {
			listmMenuinventories = (ArrayList<Menuinventory>) objMenuinventoryService.loadByHql("from Menuinventory where inventory.idinventoryID = " + inventory.getIdinventoryID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(listmMenuinventories.size()>0){
			errors.rejectValue("alreadyExistsMenu", "inventory.menu.exists");
		}
		
		ArrayList<Modifireinventory> listModifireinventories = new ArrayList<Modifireinventory>();
		
		try {
			listModifireinventories = (ArrayList<Modifireinventory>) objModifireinventoryService.loadByHql("from Modifireinventory where inventory.idinventoryID = " + inventory.getIdinventoryID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(listModifireinventories.size()>0){
			errors.rejectValue("alreadyExistsModifier", "inventory.modifier.exists");
		}
	}

}
